// pages/card/card.js
import request from '../../request/request.js'

Page({
  data: {
    keywords:'',
    cardList: []
  },
  onLoad: function (option) {
    console.log("login");
    this.getCardList();

  },
  getCardList(param) {
    request({
      url: 'card/list',
      method:'post',
      data:{category:param}
    }).then(res => {
      this.setData({
        cardList: res.data.data,
        
      })
    }).catch(err => {

    })
  },
  buyCard(event){
    var that = this
    let id =event.currentTarget.dataset.id
    let user = wx.getStorageSync("userInfo");
    if(user==''){
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration: 1500,
        mask: true
      });
      return;
    }
    wx.showModal({
      title: '提示',
      content: '您确认要购买吗?',
      success (res) {
        if (res.confirm) {
          request({
            url: 'card/buy',
            method: 'post',
            data:{
              id:id,
              userId:user.id
            }
          }).then(res => {
            if(res.data.success){
              that.getCardList()
              wx.showToast({
                title: res.data.data,
                icon: 'success',
                duration: 1800,
                mask: true
              });
            }else{
              wx.showToast({
                title: res.data.msg,
                icon: 'error',
                duration: 1800,
                mask: true
              });
            }
          })
        }
      }
    })
  },
  onSearch(){
    this.getCardList(this.data.keywords);
  },
  onChange(e){
    this.setData({keywords:e.detail})
  }
})